<template>
  <div class="new">
    <div class="main">
      <div class="header">
        邀请1人
        <el-input v-model="fixationInput" placeholder="请输入积分"></el-input>
        积分

        <div class="button">
          <div class="title" @click="plusClick(integralTotalNum)">新增</div>
        </div>
      </div>

      <div class="block" v-loading="loading">
        <div class="list" v-for="(item, index) in newList" :key="index">
          <div class="input">
            第<span v-if="item.num && item.num != Number(index + 1)">{{
              item.num
            }}</span>
            <span v-else>{{ index + 1 }}</span>
            名用户
            <el-input v-model="item.integral" :placeholder="'请输入邀请' + '第' + Number(index + 1) + '个用户'"></el-input>
            积分
          </div>

          <div class="list-img" v-if="newList.length > 1 && index != 0">
            <img src="@/assets/images/jian.png" alt="" @click="subtractClick(index, item)" />
          </div>
          <div class="list-img">
            <img src="@/assets/images/jia.png" alt="" @click="plusClick(index)" />
          </div>

          <div class="list-button" v-if="!item.num">
            <div class="title" @click="saveHit(item, index)">保存</div>
          </div>
          <div class="list-button" v-else>
            <div class="title" @click="saveHit(item, index)">修改</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  welfarePeopleList,
  welfarePeopleListAdd,
  welfarePeopleListDelete,
} from "@/api/welfare";

export default {
  data() {
    return {
      fixationInput: "",
      //---------
      newList: [],
      integralTotalNum: 0,
      loading: false
    };
  },
  created() {
    this.welfarePeopleListMet();
  },
  methods: {
    welfarePeopleListMet() {
      let params = {};

      welfarePeopleList(params).then((res) => {
        if (res.errorCode == null) {
          this.fixationInput = res.resultData.oneNewPeopleIntegral;
          res.resultData.integralList.forEach((item) => {
            item.num = Number(item.num);
          });

          this.newList = res.resultData.integralList;
          this.integralTotalNum = Number(res.resultData.totalNum + 1);
          this.loading = false
        }
      });
    },
    //-------------------------------------------
    plusClick() {
      let obj = {
        welfareVideonew: "",
      };

      this.newList.push(obj);
    },
    subtractClick(index, item) {
      this.loading = true
      if (item.welfarePeopleId) {
        let params = item.welfarePeopleId;

        welfarePeopleListDelete(params).then((res) => {
          if (res.errorCode == null) {
            this.$message.success("删除成功邀请好友人数积分！");
            this.newList.splice(index, 1);
            this.welfarePeopleListMet();
          }
        });
      } else {
        this.loading = false
        this.newList.splice(index, 1);
      }
    },

    //---------
    saveHit(item, index) {
      let params = {
        welfarePeopleId:
          item.welfarePeopleId == undefined ? "" : item.welfarePeopleId,
        num: index + 1,
        integral: item.integral,
      };
      welfarePeopleListAdd(params).then((res) => {
        if (res.errorCode == null) {
          if (item.welfarePeopleId) {
            this.$message.success("修改成功邀请好友人数积分！");
          } else {
            this.$message.success("新增成功邀请好友人数积分！");
          }

          this.welfarePeopleListMet();
        }
      });
    },
  },
};
</script>


<style lang="less" scoped>
.new {
  .main {
    .header {
      display: flex;
      align-items: center;
      padding: 0 0 10px;
      box-sizing: border-box;
      border-bottom: 1px solid #ccc;

      /deep/ .el-input {
        width: 300px;
        margin: 0 10px;
      }


      .button {
        display: flex;
        align-items: center;
        margin-left: 20px;


        .title {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 20px;
          border-radius: 5px;
          width: 80px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
      }
    }

    .block {
      margin-top: 20px;

      .list {
        display: flex;
        align-items: center;
        margin-bottom: 10px;

        /deep/ .el-input {
          width: 300px;
          margin: 0 10px;
        }

        .list-img {
          margin-left: 20px;

          img {
            width: 20px;
            height: 20px;
            cursor: pointer;
          }
        }

        .list-button {
          margin-left: 40px;
          display: flex;
          align-items: center;

          .title {
            background: #3da8f5;
            color: #fff;
            font-size: 16px;
            border-radius: 5px;
            width: 80px;
            height: 35px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>